from typing import *


class Solution:

    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        s = set()
        for x, y in obstacles:
            s.add((x, y))

        ans, x, y, d = 0, 0, 0, 0
        ds = [(0, 1), (1, 0), (0, -1), (-1, 0)]

        def max(a, b):
            return a if a > b else b

        for c in commands:
            if c == -1:
                d = (d + 1) % 4
            elif c == -2:
                d = (d + 3) % 4
            else:
                k = 0
                while k < c and (ds[d][0] + x, ds[d][1] + y) not in s:
                    x, y = ds[d][0] + x, ds[d][1] + y
                    k += 1
                ans = max(ans, x * x + y * y)
        return ans


s = Solution()
print(s.robotSim(commands=[4, -1, 3], obstacles=[]))
print(s.robotSim(commands=[4, -1, 4, -2, 4], obstacles=[[2, 4]]))
